package com.xwn.utils.core;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;

/**
 * 加密Util
 * @Author duandw
 * @Date 2019/12/31 17:40
 **/
public class AESDataEncodeUtilsImpl {
    private static final String CipherMode = "AES/ECB/PKCS5Padding";

    public AESDataEncodeUtilsImpl() {
    }

    public static int AverageRandom(int min, int max) {
        SecureRandom random1;
        SecureRandom random2;
        try {
            random1 = SecureRandom.getInstance("SHA1PRNG");
            random2 = SecureRandom.getInstance("SHA1PRNG");
        } catch (NoSuchAlgorithmException var11) {
            throw new RuntimeException(var11.getMessage(), var11);
        }

        double rand = random1.nextDouble() * random2.nextDouble();
        int randInteger = (int)(rand * 100000.0D);
        int minInteger = min * 10000;
        int maxInteger = max * 10000;
        int diffInteger = maxInteger - minInteger;
        int resultInteger = randInteger % diffInteger + minInteger;
        return (int)((double)resultInteger / 10000.0D);
    }

    public static byte[] encrypt(byte[] content, String password) {
        return encrypt(content, content.length, password);
    }

    public static byte[] encrypt(byte[] content, int length, String pw) {
        try {
            SecretKeySpec key = createKey(pw);
            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
            cipher.init(1, key);
            byte[] result = cipher.doFinal(content, 0, length);
            byteMixup(result, result.length);
            return result;
        } catch (Exception var6) {
            return null;
        }
    }

    public static byte[] decrypt(byte[] content, String pw) {
        return decrypt(content, content.length, pw);
    }

    public static byte[] decrypt(byte[] content, int length, String pw) {
        try {
            byteReset(content, length);
            SecretKeySpec key = createKey(pw);
            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
            cipher.init(2, key);
            byte[] result = cipher.doFinal(content, 0, length);
            return result;
        } catch (Exception var6) {
            return null;
        }
    }

    public static String encrypt(String content, String pw) {
        Object var2 = null;

        try {
            byte[] data = content.getBytes("UTF-8");
            return byte2hex(encrypt(data, pw));
        } catch (Exception var4) {
            return null;
        }
    }

    private static void byteMixup(byte[] data, int length) {
        exchange(data, length, 0, 32);
        exchange(data, length, 160, 320);
        exchange(data, length, 480, 1600);
        exchange(data, length, 640, 1280);
        exchange(data, length, 32000, 32768);
        exchange(data, length, 35200, 41600);
        exchange(data, length, 48000, 57728);
    }

    private static void byteReset(byte[] data, int length) {
        exchange(data, length, 0, 32);
        exchange(data, length, 160, 320);
        exchange(data, length, 480, 1600);
        exchange(data, length, 640, 1280);
        exchange(data, length, 32000, 32768);
        exchange(data, length, 35200, 41600);
        exchange(data, length, 48000, 57728);
    }

    public static void exchange(byte[] data, int length, int start, int end) {
        if (null != data) {
            if (start >= 0 && end >= 0 && end >= start) {
                if (length >= end) {
                    --end;

                    for(int i = 0; i < 32; ++i) {
                        byte temp = data[start + i];
                        data[start + i] = (byte)(~data[end - i]);
                        data[end - i] = (byte)(~temp);
                    }

                }
            }
        }
    }

    public static String decrypt(String content, String pw) {
        Object var2 = null;

        try {
            byte[] data = hex2byte(content);
            return new String(decrypt(data, pw), "UTF-8");
        } catch (Exception var4) {
            return null;
        }
    }

    public static String byte2hex(byte[] b) {
        try {
            StringBuffer sb = new StringBuffer(b.length * 2);
            String tmp = "";

            for(int n = 0; n < b.length; ++n) {
                tmp = Integer.toHexString(b[n] & 255);
                if (tmp.length() == 1) {
                    sb.append("0");
                }

                sb.append(tmp);
            }

            return sb.toString().toUpperCase();
        } catch (Exception var4) {
            return null;
        }
    }

    public static byte[] hex2byte(String inputString) {
        try {
            if (inputString != null && inputString.length() >= 2) {
                inputString = inputString.toLowerCase();
                int l = inputString.length() / 2;
                byte[] result = new byte[l];

                for(int i = 0; i < l; ++i) {
                    String tmp = inputString.substring(2 * i, 2 * i + 2);
                    result[i] = (byte)(Integer.parseInt(tmp, 16) & 255);
                }

                return result;
            } else {
                return null;
            }
        } catch (NumberFormatException var5) {
            return null;
        }
    }

    private static SecretKeySpec createKey(String value) {
        try {
            byte[] data = null;
            StringBuffer sb = new StringBuffer(16);
            if (value != null) {
                sb.append(value);
            }

            while(sb.length() < 16) {
                sb.append("0");
            }

            if (sb.length() > 16) {
                sb.setLength(16);
            }

            data = sb.toString().getBytes("UTF-8");
            return new SecretKeySpec(data, "AES");
        } catch (UnsupportedEncodingException var3) {
            return null;
        }
    }
}
